package icasue.reflect.handles.arrays;

import icasue.reflect.handles.OAble;

import java.lang.invoke.MethodHandle;
import java.lang.invoke.MethodHandles;
import java.lang.invoke.MethodType;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Stream;

/**
 * @Author: Qiao Hang
 * @CreateDate: 2020/12/3 下午5:20
 * @UpdateDate:
 * @Description:
 */
public class AryO implements OAble {

    static {
        try {
            Class<?> loadHandleOperate = AryO.class.getClassLoader().loadClass("icasue.reflect.handles.HandleSupplier");
            AryO.lookup = (MethodHandles.Lookup)loadHandleOperate.getDeclaredField("methodLookup").get(null);
        }catch (Throwable e){
            /**/
        }
    }

    static {
        findHandles();
    }

    public static MethodHandles.Lookup lookup;

    public static MethodHandle create;
    public static MethodHandle getIdx;
    public static MethodHandle setIdx;
    public static MethodHandle copy;
    public static MethodHandle stream;
    public static MethodHandle asList;

    private static void findHandles() {
        try {
            create = lookup.findStatic(Array.class,"newInstance", MethodType.methodType(Object.class,Class.class,int.class));
            getIdx = lookup.findStatic(Array.class,"get", MethodType.methodType(Object.class,Object.class,int.class));
            setIdx = lookup.findStatic(Array.class,"set", MethodType.methodType(void.class,Object.class,int.class,Object.class));
            copy = lookup.findStatic(Arrays.class,"copyOf", MethodType.methodType(Object[].class,Object[].class,int.class));
            stream = lookup.findStatic(Arrays.class,"stream", MethodType.methodType(Stream.class,Object[].class));
            asList = lookup.findStatic(Arrays.class,"asList", MethodType.methodType(List.class,Object[].class));
        }catch (Throwable e){
            e.printStackTrace();
        }
    }



}


